import Cookies from 'js-cookie'

let util = {}

util.inOf = function (arr, targetArr) { // 两数组是否有重复
  let res = true
  arr.map(item => {
    if (targetArr.indexOf(item) < 0) {
      res = false
    }
  })
  return res
}

util.oneOf = function (ele, targetArr) { // 数组是否包含
  if (targetArr.indexOf(ele) >= 0) {
    return true
  } else {
    return false
  }
}

util.isEmpty = function (obj) { // 是否为空
  if (obj === null) return true
  if (typeof obj === 'undefined') {
    return true
  }
  if (typeof obj === 'string') {
    if (obj === '') {
      return true
    }
    var reg = new RegExp('^([ ]+)|([　]+)$')
    return reg.test(obj)
  }
  return false
}

util.copyArr = function (arr) { // 复制数组
  return arr.map((e) => {
    if (typeof e === 'object') {
      return Object.assign({}, e)
    } else {
      return e
    }
  })
}

util.removeLastComma = function (str) { // 删除最后的逗号
  if (str.lastIndexOf(',') === (str.length - 1)) {
    str = str.substring(0, str.length - 1)
  }
  return str
}

util.myTrim = function (str) { // 去除前后空格
  return str.replace(/^\s+|\s+$/gm, '')
}

util.strToInt = function (str) { // 字符串转为整数
  return parseInt(str)
}

util.setToken = function (token) { // 保存token
  Cookies.set('token', token, {expires: 1})
}

util.getToken = function () { // 获取token
  const token = Cookies.get('token')
  if (token) return token
  else return false
}

util.setUser = function (user) { // 保存用户信息
  Cookies.set('user', user, {expires: 1})
}

util.getUser = function () { // 获取token
  const user = Cookies.get('user')
  if (user) return user
  else return false
}

/**
* @desc 退出登录
*/
util.logout = function () {
 sessionStorage.user = ''
 sessionStorage.removeItem('aselect')
 sessionStorage.removeItem('anavList')
 sessionStorage.removeItem('aselected')
 sessionStorage.removeItem('aname')
}

util.getUnion = function (arr1, arr2) {
  return Array.from(new Set([...arr1, ...arr2]))
}

util.findNodeUpperByClasses = function (ele, classes) {
  let parentNode = ele.parentNode
  if (parentNode) {
    let classList = parentNode.classList
    if (classList && classes.every(className => classList.contains(className))) {
      return parentNode
    } else {
      return this.findNodeUpperByClasses(parentNode, classes)
    }
  }
}
util.getCurrentDate = function () { // 获取当前日期
  let date = new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month < 10) {
    month = '0' + month
  }
  let day = date.getDate()
  if (day < 10) {
    day = '0' + day
  }
  let hour = date.getHours()
  let minutes = date.getMinutes()
  let second = date.getSeconds()
  return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + second
  // return date.toLocaleString('zh', {hour12: false})
}
util.fommotDate = function (date) { // 获取当前日期
	let date1 = new Date(date).toString().replace(/-/g, '/')
	let date2 = new Date(+new Date(date1) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
	return date2
}
export default util
